<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Circle extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    public function index($sequence = "") {
        $html = $this->init("基礎資料-商圈", "jquery,pagination,base_data", "table");
        $this->load->model('circle_model');
        $keyword = "";
        $config['per_page'] = 10;
        $config['total_rows'] = $this->circle_model->get_all_count();
        $config['url'] = '/circle/page_ajax';
        $config['num_pages'] = ceil($config['total_rows'] / $config['per_page']);
        $limit = $config['per_page'] . ",0";
        $html['page'] = $config;
        $html['sequence'] = $sequence;
        $html["circle"] = $this->circle_model->query_keyword($keyword, $limit);
        $this->load->view('circle', $html);
    }

    public function page_ajax() {
        $this->load->model('circle_model');
        $page = $this->input->post('page');
        $sequence = $this->input->post('sequence');
        $keyword = "";
        $html["edit_mode"] = "";
        $config['per_page'] = 10;
        $p = $config['per_page'] * ($page - 1);
        $limit = $config['per_page'] . "," . $p;
        $html["result"] = $this->circle_model->query_keyword($keyword, $limit);
        $this->load->view('circle_ajax', $html);
    }

    public function add() {
        $this->load->model('circle_model');
        $data['city'] = $this->input->post('city');
        $data['area'] = $this->input->post('area');
        $data['value'] = $this->input->post('value');
        $data['sequence'] = $this->input->post('sequence');
        $return_result = $this->circle_model->add($data);
        if ($return_result > 0) {
            redirect("/circle");
        } else {
            echo "插入失敗";
        }
    }

    public function edit_ajax() {
        $this->load->model('circle_model');
        $id = $this->input->post('circle_id');
        $html["edit_mode"] = "edit";
        $html["info"] = $this->circle_model->get_info($id);
        $this->load->view('circle_ajax', $html);
    }

    public function get_circle_josn() {
        $this->load->model('circle_model');
        $query = trim($this->input->get('query'));
        $circle = $this->circle_model->get_demand_circle($query);
        echo urldecode(json_encode($circle));
    }

    public function edit_submit_ajax() {
        $this->load->model('circle_model');
        $data['id'] = $this->input->post('circle_id');
        $data['city'] = $this->input->post('city');
        $data['area'] = $this->input->post('area');
        $data['value'] = $this->input->post('value');
        $data['sequence'] = $this->input->post('sequence');
        $data['mtime'] = time();
        $return_result = $this->circle_model->update_data($data);
        if ($return_result) {
            $html["edit_mode"] = "edit_return";
            $html["info"] = $this->circle_model->get_info($data['id']);
            $this->load->view('circle_ajax', $html);
        } else {
            echo "0";
        }
    }

    public function cancel_edit_ajax() {
        $this->load->model('circle_model');
        $id = $this->input->post('circle_id');
        $html["edit_mode"] = "edit_return";
        $html["info"] = $this->circle_model->get_info($id);
        $this->load->view('circle_ajax', $html);
    }

    public function data_delete($id = null) {
        $this->load->model('circle_model');
        if ($id != null) {
            $data["id"] = $id;
            $return_result = $this->circle_model->del($data);
            if ($return_result) {
                redirect("/circle");
            } else {
                echo "刪除失敗";
            }
        } else {
            echo "刪除失敗";
        }
    }

    public function area_circle_ajax() {

        $this->load->model('circle_model');
        $data['city'] = $this->input->post('circle_city');
        $data['area'] = $this->input->post('circle_area');
        $html["circle"] = $this->circle_model->get_area_circle($data);
        $this->load->view('area_circle_ajax', $html);

    }

    public function circle_josn() {

        $this->load->model('circle_model');
        
        $city = $this->input->post('city');
        $area = $this->input->post('area');

        

        $html["data"] = $this->circle_model->get_country_circle_victor2($area , $city );        

        $this->load->view('circle_josn', $html);
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */